<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applicationTime">
        <el-date-picker clearable
          v-model="queryParams.applicationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item label="更改时间" prop="reimbursementAmount">
        <el-date-picker clearable
          v-model="queryParams.reimbursementAmount"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini" round
          @click="handleAdd"
          v-hasPermi="['official:finance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini" round
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['official:finance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini" round
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['official:finance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini" round
          @click="handleExport"
          v-hasPermi="['official:finance:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-refresh-right"
          size="mini"
          @click="handleRollback"
          v-hasPermi="['official:finance:export']"
        >回滾</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="financeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="financeId" />
      <el-table-column label="报销内容" align="center" prop="reimbursementContent" />
      <el-table-column label="报销金额" align="center" prop="money" />
      <el-table-column label="申请人" align="center" prop="name" />
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更改时间" align="center" prop="reimbursementAmount" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reimbursementAmount, '{y}-{m}-{d}') }}</span>
           {{scope.row.reimbursementAmount==null?'暂无更改时间':''}}
        </template>
      </el-table-column>
       <el-table-column label="审核状况" align="center" prop="audit">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.audit==1">通过</el-tag>
          <el-tag type="danger" v-if="scope.row.audit==2">未通过</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['official:finance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['official:finance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务报销管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="报销内容">
          <editor v-model="form.reimbursementContent" :min-height="192"/>
        </el-form-item> -->
        <el-form-item label="申请人" prop="name">
          <el-input v-model="form.name" :disabled="verification1" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="报销金额" prop="money">
          <el-input v-model="form.money" :disabled="verification1" placeholder="请输入报销金额" />
        </el-form-item>
        <el-form-item label="报销内容" prop="reimbursementContent">
          <el-input v-model="form.reimbursementContent" type="textarea" placeholder="请输入报销内容"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>
        
        <el-form-item label="申请时间" prop="applicationTime">
          <el-date-picker clearable
            v-model="form.applicationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>  
         <el-form-item label="审核情况" prop="audit">
          <el-select v-model="form.audit" placeholder="请选择">
            <!--          clearable-->
            <!--          @keyup.enter.native="handleQuery">-->
            <el-option
              v-for="item in audits"
              :key="item.audit"
              :label="item.auditName"
              :value="item.audit">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFinance, getFinance, delFinance, addFinance, updateFinance,rollback } from "@/api/official/finance";

export default {
  verification1:true,
  name: "Finance",
  data() {
    return {
      formLabelWidth: '80px',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务报销管理表格数据
      financeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reimbursementContent: null,
        applicationTime: null,
        name: null,
        reimbursementAmount: null,
        money: null,
        audit:null
      },
       audits: [
        { audit: 1, auditName: "通过" },
        { audit: 2, auditName: "未通过" }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        audit : [
          { required: true, message: "审核情况不能为空", trigger: "blur" }
        ],
        reimbursementContent: [
          { required: true, message: "报销内容不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "报销金额不能为空", trigger: "blur" }
        ],
         name: [
          { required: true, message: "申请人不能为空", trigger: "blur" },
          {
            validator: function(rule, value, callback) {
              //校验中文的正则：/^[\u4e00-\u9fa5]{0,}$/
              // ^[a-zA-Z\u4e00-\u9fa5]+$
              if (/^[a-zA-Z\u4e00-\u9fa5]+$/.test(value) == false) {
                callback(new Error("请输入中文或英文"));
              } else {
                //校验通过
                callback();
              }
            },
            trigger: "change"
          }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询财务报销管理列表 */
    getList() {
      this.loading = true;
      listFinance(this.queryParams).then(response => {
        this.financeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        financeId: null,
        reimbursementContent: null,
        applicationTime: null,
        name: null,
        reimbursementAmount: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.financeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.verification1 = false;
      this.reset();
      this.open = true;
      this.title = "添加财务报销管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.verification1 = true;
      this.reset();
      const financeId = row.financeId || this.ids
      getFinance(financeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改财务报销管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.financeId != null) {
            updateFinance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFinance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const financeIds = row.financeId || this.ids;
      this.$modal.confirm('是否确认删除财务报销管理编号为"' + financeIds + '"的数据项？').then(function() {
        return delFinance(financeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('official/finance/export', {
        ...this.queryParams
      }, `finance_${new Date().getTime()}.xlsx`)
    },

    handleRollback(){
      rollback().then((res)=>{
        if (!res){
          this.$message({
            message: '目前不存在撤销功能',
            type: 'warning'
          });
        }else {
          this.getList();
        }
      })
    }
  }
};
</script>
